{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-03-03T13:08:16.141722Z",
     "start_time": "2025-03-03T13:08:15.016908Z"
    }
   },
   "source": [
    "import os\n",
    "\n",
    "from sklearn.datasets import fetch_california_housing\n",
    "from sklearn.linear_model import LinearRegression, SGDRegressor, Ridge, LogisticRegression, Lasso\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.metrics import mean_squared_error, classification_report, roc_auc_score\n",
    "import joblib\n",
    "import pandas as pd\n",
    "import numpy as np"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T13:08:16.145746Z",
     "start_time": "2025-03-03T13:08:16.143232Z"
    }
   },
   "cell_type": "code",
   "source": "sgd = SGDRegressor(eta0=0.01,penalty='l2', max_iter=1000)",
   "id": "ff44a28a615220f5",
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T13:08:16.167342Z",
     "start_time": "2025-03-03T13:08:16.146891Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 获取数据\n",
    "fe_cal = fetch_california_housing(data_home='data')\n",
    "\n",
    "# 分割数据集到训练集和测试集\n",
    "x_train, x_test, y_train, y_test = train_test_split(fe_cal.data, fe_cal.target, test_size=0.25, random_state=1)"
   ],
   "id": "30c4e8f37d81d12e",
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T13:08:16.217052Z",
     "start_time": "2025-03-03T13:08:16.168359Z"
    }
   },
   "cell_type": "code",
   "source": [
    "sgd.fit(x_train, y_train)\n",
    "#\n",
    "print('梯度下降的回归系数', sgd.coef_)"
   ],
   "id": "3d3f647fc39821bc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "梯度下降的回归系数 [ 2.41305487e+10 -3.17482640e+11 -1.98504345e+11 -1.31287783e+11\n",
      " -4.38146146e+11  9.88610796e+10 -2.31226726e+11 -9.85025087e+10]\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T13:08:16.221714Z",
     "start_time": "2025-03-03T13:08:16.218624Z"
    }
   },
   "cell_type": "code",
   "source": "# 明显不对，发现忘记标准化了",
   "id": "45e9cfa82322009f",
   "outputs": [],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T13:08:16.230318Z",
     "start_time": "2025-03-03T13:08:16.223090Z"
    }
   },
   "cell_type": "code",
   "source": [
    "std_x = StandardScaler()\n",
    "x_train = std_x.fit_transform(x_train) \n",
    "x_test = std_x.transform(x_test) #测试集标准化\n",
    "std_y = StandardScaler()"
   ],
   "id": "563bd93a9105c9a7",
   "outputs": [],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T13:08:16.249766Z",
     "start_time": "2025-03-03T13:08:16.231555Z"
    }
   },
   "cell_type": "code",
   "source": [
    "sgd.fit(x_train, y_train)\n",
    "y_prediction = sgd.predict(x_test)\n",
    "print('梯度下降的回归系数', sgd.coef_)\n",
    "print('均方误差：',mean_squared_error(y_test,y_prediction))"
   ],
   "id": "7b766f9a3312a8e2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "梯度下降的回归系数 [ 0.827503    0.13325558 -0.22328597  0.30537277  0.00333817 -0.01892918\n",
      " -0.90942462 -0.86761442]\n",
      "均方误差： 0.5371945918844669\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T13:08:16.252613Z",
     "start_time": "2025-03-03T13:08:16.251077Z"
    }
   },
   "cell_type": "code",
   "source": "# 均方误差： 0.5371945918844669",
   "id": "4f3bc8cf96989f45",
   "outputs": [],
   "execution_count": 7
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
